<?php

/*
 * status 0为待确认
 * status 1为已确认
 * status 2为部分退货
 * status 3为部分退货
 * */

class Supplier_account_logModel extends Model
{
    //列表
    public function _list($view, $map, $sort = 'sort', $pagesize = 20)
    {
        //计算总页数
        $count = $this->where($map)->count();
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);
        $list = $this->where($map)->order($sort)->limit($p->firstRow . ',
' . $p->listRows)->select();
        $pages = $p->show();
        $view->assign('list', $list);
        $view->assign('pages', $pages);

    }

    //列表
    public function _listgroup($view, $map, $sort = 'sort', $pagesize = 20)
    {
        //计算总页数
        $count = M() -> query("select count(*) as tp from ".$this->where($map)->group('order_id,product_id,attr')->buildSql().' as a');
		$count = $count[0]['tp'];
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);
        $list = $this->where($map)->order($sort)->limit($p->firstRow . ',
' . $p->listRows)->group('order_id,product_id,attr')->Field('sum(changenum) as changenum, order_sn, dateline, status, order_id, attr, product_id')->select();
        $pages = $p->show();
        foreach ($list as $k => $v) {

            $list[$k]['ostatus'] = M('order')->where(array('id' => $v['order_id']))->getField('status');
        }
        $view->assign('list', $list);
        $view->assign('pages', $pages);
    }

    //更新日志表信息
    public function up_account_log($member_id, $data)
    {
        $data['member_id'] = $member_id;
        $mes = $this->add($data);
        if ($mes) {
            return true;
        } else {
            return false;
        }

    }


    //改变用户余额
    public function change($num = 0, $supplier_id = 0, $remark = '', $pdstate = 0)
    {
        
		$site_id = __SITEID__;
		$data['changenum'] = $num;
		$data['site_id']=__SITEID__;
		$data['user_id'] = session(C('USER_AUTH_KEY') );
		$data['supplier_id'] = $supplier_id;
		$data['remark'] = $remark;
		$data['dateline'] = time();
		
		$ret = $this-> data($data) -> add();
        if($ret){
            //判断supplier_id是否有对应的member_id，如果有，往供货商账户member_account_log，写入金额，备注'供货商结算'
            $sup = M('Member_supplier')->where(array('supplier_id'=>$data['supplier_id'],'site_id'=>$site_id))->find();
			
			
			
			
            if($sup){
                $member['site_id']      =$site_id;//网点
                $member['member_id']    = $sup['member_id'];//会员号
                $member['datetime']     =time();//时间
                $member['remark']       ='供货商结算';//备注
                $member['changenum']    =$data['changenum'];//金额
                $memberAdd=D('Member_account_log')->add($member);//member_account_log 表添加一条数据
                if($memberAdd){									//往member表写入数据！！！
                    $oldmoney=D('Member')->where(array('id'=>$member['member_id']))->getField('money');
                    $default['money'] = $member['changenum']+$oldmoney;
                    $count=D('Member')->where(array('id'=>$member['member_id']))->save($default);
                    if($count){
                        return true;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
				return true;
            }
        }else{
            return false;
        }
	
    }


    //确认一个订单分成日志
    public function confirm($order_id, $product_id = 0, $attr = '')
    {
        $map['order_id'] = $order_id;
        if ($product_id > 0) {
            $map['product_id'] = $product_id;
        }
        if ($attr != '') {
            $map['attr'] = $attr;
        }
		
		$map['status'] = 0;
		
        $ret = $this->where($map)->data(array('status' => 1))->save();
        if ($ret > 0) {
			$map['status'] = 1; 
            $memberDb = D('Member');
            $member_id = $this->where($map)->select();
            foreach ($member_id as $k => $v) {
                $memberDb->where(array('id' => $v['member_id']))->setInc('money', $v['changenum']);
            }
            return true;
        } else {
            return false;
        }
    }


    //取消一个订单分成日志
    public function cancel($order_id, $product_id = 0, $attr = '', $num = 1)
    {
        $map['order_id'] = $order_id;
        if ($product_id > 0) {
            $map['product_id'] = $product_id;
        }
        if ($attr != '') {
            $map['attr'] = $attr;
        }
        $malog = $this->where($map)->find();
        $data['changenum'] = $malog['changenum'] - ($malog['changenum'] / $malog['num']) * $num;
        $data['num'] = $malog['num'] - $num;
        if ($data['num'] > 0) {
            //部分退货
            $data['status'] = 2;
        } else {
            //全部退货
            $data['status'] = 3;
        }
        if ($data['num'] >= 0) {
            $ret = $this->where($map)->data($data)->save();
        }
        if ($ret !== false) {
            return true;
        } else {
            return false;
        }
    }
}
